import cTitle from "components/title";
import { scrollMixin } from '../../utils/mixin';
// import { Toast } from 'vant';
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      rotation: [],
      recommend: [],
      loading: false,
      allLoaded: false,
      goload: true,
      isLoadMore: true,
      page: 1,
      total_page: 0,
      datas: [],
      tabArr: ["全部", "竞拍中", "即将开始", "已结束"],
      activeName: 0,
      lsitData: [],

      //新样式的数据
      styleType: 2,
      auctionColor: "#f5f5f5",
      auctionImg: "",
      beginStatus: 0,
      loadUp:false,
      swiperOption: {
        direction: "horizontal",
        autoHeight: true,
        autoplay: false,
        loop: true,
        initialSlide: 0,
        pagination: {
          el: ".swiper-pagination"
        }
      }
    };
  },
  mounted() {
    window.scrollTo(0, 0); //页面向上滚动
    this.getData();
    this.getNew();
    this.now = new Date(this.timestampToTime(new Date().valueOf() / 1000)).valueOf() / 1000;

    // if (this.styleType === 2) {
    //   this.newAuction();
    // }
  },
  filters: {
    capitalize(value) {
      if (value) {
        return value;
      } else {
        return "0.00";
      }
    }
  },
  computed: {
    computingTime() {
      return function(afterTime, backTime) {
        //处理未开始
        let startTime = afterTime * 1000;
        let endTime = backTime * 1000;
        let nowtime = new Date().getTime();
        if (startTime > nowtime) {
          //未开始
          return -1;
        } else if (nowtime >= startTime && nowtime < endTime) {
          //竞拍中
          return 0;
        } else {
          //竞拍结束
          return 1;
        }
      };
    }
  },
  methods: {
    TapChange() {
      console.log(this.activeName, "ddddddd");
      this.initData();
      if (this.activeName != 0) {
        this.newListData();
      } else {
        this.getNew();
      }
    },
    newListData() {
      let json = {
        auction_type: this.activeName
      };
      $http
        .get("plugin.auction.api.index.index-page", json, "获取中")
        .then(response => {
          if (response.result == 1) {
            this.lsitData = response.data.list.data;
            this.total_page = response.data.list.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    initData() {
      this.loading = false;
      this.allLoaded = false;
      this.goload = true;
      this.isLoadMore = true;
      this.page = 1;
      this.total_page = 0;
      this.datas = [];
      this.lsitData = [];
    },
    //日期转化
    timestampToTime(timestamp) {
      var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "/";
      let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "/";
      let D = this.change(date.getDate()) + " ";
      let h = this.change(date.getHours()) + ":";
      let m = this.change(date.getMinutes());
      // let s = this.change(date.getSeconds());
      return Y + M + D + h + m;
    },
    change(t) {
      if (t < 10) {
        return "0" + t;
      } else {
        return t;
      }
    },
    toMore() {
      this.$router.push(this.fun.getUrl("auctionMore"));
    },
    toSearch() {
      this.$router.push(this.fun.getUrl("auctionSearch"));
    },
    toDetail(item) {
      let kid = item.member_id ? item.member_id : item.auctioneer_id;
      this.$router.push(this.fun.getUrl("auctionGood", { goods_id: item.goods_id, id: kid }));
    },
    toMy() {
      this.$router.push(this.fun.getUrl("myIndex"));
    },
    //获取数据
    getData() {
      let json = {
        art: "index"
      };
      $http
        .get("plugin.auction.api.index.getAuctionData", json, "获取中")
        .then(response => {
          if (response.result == 1) {
            let data = response.data;
            this.rotation = data?.index.rotation;
            this.recommend = data?.index.recommend;
            this.styleType = data?.auction_style.auction_style;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    slideNavTo(url) {
      if (url) {
        window.location.href = url;
      }
    },
    getNew() {
      var that = this;
      let json = {
        keyword: "",
        status: 1,
        auc_status: "",
        is_new: 1,
        is_recommend: "",
        auctioneer_id: "",
        art: "getSearch",
        page: this.page,
        is_pass: "0"
      };
      $http
        .get("plugin.auction.api.index.getAuctionData", json, "获取中")
        .then(response => {
          if (response.result == 1) {
            that.total_page = response.data.getSearch.last_page;
            if (!that.total_page) {
              that.total_page = 0;
            }
            if (that.page === that.total_page) {
              that.loadUp = true;
            }
            let data = response.data;
            that.datas = response.data.getSearch.data;
            let auctionStyle = data?.auction_style;
            this.styleType = auctionStyle.auction_style;
            this.auctionColor = auctionStyle.auction_style_color;
            this.auctionImg = auctionStyle.auction_style_image;
          } else {
            that.$dialog.alert({ message: response.msg });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

    getMoreData() {
      var that = this;
      this.isLoadMore = false;
      if (this.page === this.total_page) {
        return;
      }
      if (this.page >= this.total_page) {
        console.log("getMoreData", this.total_page);
        that.loading = true;
        that.allLoaded = true;
        return;
      } else {
        if (this.activeName == 0) {
          this.page = this.page + 1;
          let json = {
            keyword: "",
            status: "1",
            auc_status: "",
            is_new: 1,
            is_recommend: "",
            auctioneer_id: "",
            art: "getSearch",
            page: this.page,
            is_pass: "0"
          };
          $http.get("plugin.auction.api.index.getAuctionData", json, "加载中").then(
            function(response) {
              if (response.result === 1) {
                that.total_page = response.data.getSearch.last_page;
                that.loading = false;
                that.allLoaded = false;
                that.isLoadMore = true;
                if (that.page === that.total_page) {
                  that.loadUp = true;
                }
                that.datas = [...that.datas, ...response.data.getSearch.data];
              } else {
                that.page = that.page - 1;
                that.loading = true;
                that.allLoaded = true;
                that.isLoadMore = false;
                return;
              }
            },
            function(response) {
              // error callback
            }
          );
        } else {
          let json = {
            auction_type: this.activeName,
            page: this.page
          };
          $http.get("plugin.auction.api.index.index-page", json, "加载中").then(response => {
            if (response.result === 1) {
              this.isLoadMore = true;
              this.lsitData = this.lsitData.concat(response.data.list.data);
              this.total_page = response.data.list.last_page;
              if (!this.total_page) {
                this.total_page = 0;
              }
            } else {
              this.page = this.page - 1;
              this.loading = true;
              this.allLoaded = true;
              this.isLoadMore = false;
              return;
            }
          });
        }
      }
    }
  },
  components: {
    cTitle
  }
};
